layui.config({
    base: $config.resUrl+'other/'//定义基目录
}).extend({
    ajaxExtention:'js/ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
    $tool:'js/tool',
    $api:'js/api'
}).use(['form','layer','laydate','ajaxExtention','$tool','$api'], function() {
	var $ = layui.jquery, 
	laydate = layui.laydate,
	dtree = layui.dtree,
	form = layui.form,
	layer = layui.layer,
	$tool = layui.$tool,
    $api = layui.$api;
	
	//日期控件
	laydate.render({
		elem : '#createTime'
	});
	laydate.render({
		elem : '#updateTime'
	});
	
	//获取树形结构单击时传给子界面的参数
	var getUrlParam = function(name) {
	  var url = location.search;
	  url = url.substring(url.indexOf("?"));
	  if (url.indexOf("?") != -1) {
	    var str = url.substr(1);
	    strs = str.split("&");
	    for ( var i = 0; i < strs.length; i++) {
	      if (name == strs[i].split("=")[0]) {
	        return unescape(strs[i].split("=")[1]);
	      }
	    }
	  }
	  return "";
	};
	
	//界面中存储的当前机构ID
	var menuId = getUrlParam("nodeId");//$(window.parent.document).find("#deptId").val();
	//请求参数
    var req = {
    	id : menuId
    };
    //调用ajax方法，
	$api.MenuDetails(req,function (data) {
		if (false == data.ret) {
			layer.msg(data.msg, {
				time : 3000
			});
			return;
		};
		form.val("show_form", {
			"id" : data.data.id ,
			"name" : data.data.name,
			"parentId" : data.data.parentId,
			"createUser" : data.data.operator,
			"createTime" : $tool.getMyDate(data.data.operateTime),
			"level" : getUrlParam("level"),
			"seq" : data.data.seq,
			"remark" : data.data.remark,
			"href" : data.data.href,
			"requestUrl" : data.data.requestUrl
		});
		
		//渲染上级机构名称
		$api.MenuDetails({id:data.data.parentId},function(result){
			if (true == result.ret) {
				$("#parentName").val(result.data.name);
				return;
			};
			$("#parentName").val("");
		},function(error){
			layer.msg(error.msg, {
				time : 3000
			});
		});
		
		if("1" == data.data.status){
			$("#status").attr("checked","checked");
		}
    },function(date){
    	layer.msg(data.msg, {
			time : 3000
		});
    });
	
  //监听提交按钮
  form.on('submit(menuEditSubmit)', function(data){
	  $api.MenuUpdate(data.field,function (result) {
    	  layer.msg(result.msg,{time: 3000},function(){
    		  parent.location.reload();
    	  });
    	  
      },function (result) {
    	  layer.msg(JSON.stringify(result));
      });
    return false;
  });
  
  //机构弹出框
  $("#parentName").click(function(){
	  layer.open({
	    type: 1,
	    title: "菜单列表",
	    area: ["350px", "80%"],
	    content: '<ul id="inMenuTree" class="dtree" data-id="0"></ul>',
	    btn: ['确认选择'],
	    success: function(layero, index){
	      dtree.render({
	        elem: "#inMenuTree",
	        url: "/sys/menu/tree.json",
	        dot: false
	      });
	      
	      // 绑定节点的双击
	      dtree.on("nodedblclick('inMenuTree')", function(obj){
	        $("#parentId").val(obj.param.nodeId);
	        $("#parentName").val(obj.param.context);
	        $("#level").val(obj.param.level);
	        layer.close(index);
	      });
	    },
	    yes: function(index, layero) {
	      var param = dtree.getNowParam("inMenuTree"); // 获取当前选中节点
	      $("#level").val(param.level);
	      $("#parentId").val(param.nodeId);
	      $("#parentName").val(param.context);
	      layer.close(index);
	    }
	  });
	});
});
